<template>
  <!--明细页面弹出框-->
  <el-dialog
    :title="this.formTitle"
    center
    :visible.sync="open"
    width="1024px"
    :close-on-click-modal="true"
    @close="closeFn"
    v-if = "open"
  >
    <el-form ref="detailsForm" :model="formData" :rules="rules" label-width="120px" :disabled="formDisabled">
      <el-row>
        <el-col :span="12">
          <el-form-item label="交易流水号：" prop="id">
            <el-input v-model="formData.id" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="票据号码：" prop="draftNo">
            <el-input v-model="formData.draftNo" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="票据类型：" prop="draftType">
            <el-select style="width: 100%" disabled v-model="formData.draftType" placeholder="">
              <el-option
                v-for="dict in dictMap.DRAFT_TYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务类型：" prop="bizTypeCode">
            <el-select v-model="formData.bizTypeCode" disabled placeholder="" style="width: 100%">
              <el-option
                v-for="dict in dictMap.BIZ_TYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="出票行行号：" prop="issuedBrno">
            <el-input
              v-model="formData.issuedBrno"
              disabled
              @change="checkbankcode('issuedBrno')"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-row>
            <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('issuedBrno')">
              行名行号查询
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="出票行行名：" prop="issuedBrname">
            <el-input v-model="formData.issuedBrname" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <div v-if="this.formData.draftType == '0'">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="代理付款行行号：" prop="agentBrno">
              <el-input v-model="formData.agentBrno" @change="checkbankcode('agentBrno')" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-row>
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('agentBrno')">
                行名行号查询
              </el-button>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="代理付款行行名：" prop="agentBrname">
              <el-input v-model="formData.agentBrname" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出票人账号：" prop="drawerAccount">
            <el-input v-model="formData.drawerAccount" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出票人名称：" prop="drawerName">
            <el-input v-model="formData.drawerName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="申请人账号：" prop="issuedActno">
            <el-input v-model="formData.issuedActno" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请人名称：" prop="issuedName">
            <el-input v-model="formData.issuedName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收款人账号：" prop="payeeActno">
            <el-input v-model="formData.payeeActno" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款人名称：" prop="payeeName">
            <el-input v-model="formData.payeeName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原收款人名称：" prop="oriPayeeName">
            <el-input v-model="formData.oriPayeeName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="票据种类：" prop="draftCode">
            <el-select style="width: 100%" v-model="formData.draftCode" placeholder="">
              <el-option
                v-for="dict in dictMap.DRAFT_CODE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="记账标志：" prop="cbflag">
            <el-select style="width: 100%" v-model="formData.cbflag" placeholder="">
              <el-option
                v-for="dict in dictMap.CBFLAG"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出票日期：" prop="issuedDate">
            <el-date-picker
              v-model="formData.issuedDate"
              type="date"
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出票金额：" prop="issuedAmount">
            <HtAmountInput v-model="formData.issuedAmount" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="提示付款日期：" prop="payDate">
            <el-date-picker
              v-model="formData.payDate"
              type="date"
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="汇票到期日：" prop="draftMaturity">
            <el-date-picker
              v-model="formData.draftMaturity"
              type="date"
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="兑付日期：" prop="workdate">
            <el-date-picker
              v-model="formData.workdate"
              type="date"
              disabled
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结算金额：" prop="payAmount">
            <HtAmountInput v-model="formData.payAmount" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="支付密码：" prop="payPassword">
            <el-input v-model="formData.payPassword" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密押：" prop="macKey">
            <el-input v-model="formData.macKey" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="背书人数：" prop="endorserNumber">
            <el-input v-model="formData.endorserNumber" placeholder="" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="背书人名称：" prop="endorserName">
            <el-input v-model="formData.endorserName" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24">
          <el-form-item label="用途：" prop="remarks">
            <el-input type="textarea" v-model="formData.remarks" maxlength="256" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-show="!formDisabled" :loading="isLoading">提交</el-button>
      <el-button type="warning" @click="reset" v-show="!formDisabled" :loading="isLoading">重置</el-button>
      <el-button @click="closeFn" >取消</el-button>
    </div>
    <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
  </el-dialog>
</template>
<script>
import { submit } from "@/api/cnaps/bepsDraft/bank_note_pay_update.js";

import BankBranchDialog from "@/views/components/BankBranchDialog";
import HtAmountInput from "@/views/components/HtAmountInput";
export default {
  name: "InsideTransferApproveDetails",
  components: { BankBranchDialog, HtAmountInput },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    "formData.endorserName": {
      handler(val) {
        if (val !== '' && val != undefined) {
          this.$set(this.formData, "endorserNumber", "1")
          this.$set(this.rules.oriPayeeName[0], "required", true);
        } else {
          this.$set(this.formData, "endorserNumber", "0")
          this.$set(this.rules.oriPayeeName[0], "required", false);
        }
      },
    },
  },
  data() {
    return {
      formDisabled: false,
      formTitle: "",
      // 是否显示弹出层
      open: false,
      isLoading: false,
      // 录入参数

      rules: {
        draftNo: [{ required: true, message: "不能为空", trigger: "blur" }],
        draftType: [{ required: true, message: "不能为空", trigger: "change" }],
        bizTypeCode: [{ required: true, message: "不能为空", trigger: "change" }],
        issuedBrno: [{ required: true, message: "不能为空", trigger: "change" }],
        issuedBrname: [{ required: true, message: "不能为空", trigger: "change" }],
        drawerAccount: [{ required: true, message: "不能为空", trigger: "blur" }],
        drawerName: [{ required: true, message: "不能为空", trigger: "blur" }],
        issuedActno: [{ required: true, message: "不能为空", trigger: "blur" }],
        issuedName: [{ required: true, message: "不能为空", trigger: "blur" }],
        payeeActno: [{ required: true, message: "不能为空", trigger: "blur" }],
        payeeName: [{ required: true, message: "不能为空", trigger: "blur" }],
        issuedDate: [{ required: true, message: "不能为空", trigger: "change" }],
        issuedAmount: [{ required: true, message: "不能为空", trigger: "blur" }],
        workdate: [{ required: true, message: "不能为空", trigger: "change" }],
        payAmount: [{ required: true, message: "不能为空", trigger: "blur" }],
        amount: [{ required: true, message: "不能为空", trigger: "blur" }],
        macKey: [{ required: true, message: "不能为空", trigger: "blur" }],
        oriPayeeName : [ { required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    //行名行号返回
    confirmBankBranch(fromFlag, row) {
      if (fromFlag == "issuedBrno") {
        this.$set(this.formData, "issuedBrno", row.bankCode);
        this.$set(this.formData, "issuedBrname", row.orgFullname);
      } else if (fromFlag == "agentBrno") {
        this.$set(this.formData, "agentBrno", row.bankCode);
        this.$set(this.formData, "agentBrname", row.orgFullname);
      }
    },
    //行名行号查询
    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },

    //审核
    submitForm() {
      this.$refs["detailsForm"].validate((valid) => {
        if (valid) {
          this.isLoading = true;
          submit({ ...this.formData })
            .then((res) => {
              this.isLoading = false;
              this.$emit("editComplete", true);
              this.msgSuccess("成功!");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
              //   this.$alert(`${res.result}`, "系统提示", {
              //     confirmButtonText: "确认",
              //     type: "error",
              //     callback: (action) => {},
            });
        }
      });
    },

    // 表单重置
    reset() {
      this.resetForm("detailsForm");
    },
    closeFn() {
      this.open = false;
      this.reset();
    },
    show(type) {
      this.formTitle = "银行票据兑付" + type;
      this.formDisabled = type === "明细" ? true : false;
      this.open = true;
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
